package com.tarzan.system.controller;

import com.tarzan.api.model.response.ment.RouterVo;
import com.tarzan.api.model.response.user.UserRolePermissionsInfoResp;
import com.tarzan.common.core.base.BaseResponse;
import com.tarzan.common.core.exception.BusinessException;
import com.tarzan.system.service.ISysMenuService;
import com.tarzan.system.service.ISysUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Package com.tarzan.system.controller
 * @Title SysIndexController
 * @Description TODO
 * @Author liujinshan
 * @CreateTime 2021/8/12 13:37
 */
@RestController
@RequestMapping("tarzan")
public class SysIndexController {

    private final static Logger logger = LoggerFactory.getLogger(SysIndexController.class);

    @Autowired
    private ISysMenuService iSysMenuService;

    @Autowired
    private ISysUserService iSysUserService;


    @GetMapping("info")
    public BaseResponse<UserRolePermissionsInfoResp> getUserInfo() {
        String  userName = SecurityContextHolder.getContext().getAuthentication().getName();
        BaseResponse<UserRolePermissionsInfoResp> response  = new BaseResponse<>();
        try {
            response  = BaseResponse.ok(iSysUserService.getUserInfo(userName));
        } catch (BusinessException e) {
            response.setErrCode(e.getCode());
            response.setErrMsg(e.getMsg());
            logger.error("SysIndexController -> getUserInfo() BusinessException ex="+e);
        } catch (Exception e) {
            response.setErrCode("9999");
            response.setErrMsg(e.getMessage());
            logger.error("SysIndexController -> getUserInfo() BusinessException ex="+e);
        }
        return response;
    }



    @GetMapping("getRouters")
    public BaseResponse<List<RouterVo>> getRouters()
    {
        BaseResponse<List<RouterVo>> response  = new BaseResponse<>();
        String  userName = SecurityContextHolder.getContext().getAuthentication().getName();
        try {
            response =  BaseResponse.ok(iSysMenuService.buildMenus(userName));
        } catch (BusinessException e) {
            response.setErrCode(e.getCode());
            response.setErrMsg(e.getMsg());
            logger.error("SysIndexController -> getRouters() BusinessException ex="+e);
        } catch (Exception e) {
            response.setErrCode("9999");
            response.setErrMsg(e.getMessage());
            logger.error("SysIndexController -> getRouters() BusinessException ex="+e);
        }
        return response;
    }


    @PostMapping(value = "outLogin")
    public BaseResponse outLogin(@RequestHeader(value = HttpHeaders.AUTHORIZATION, required = false) String authHeader){
        try{
            iSysMenuService.removeLoginInfoByToken(authHeader);
        } catch (BusinessException e) {
            return BaseResponse.error(e.getCode(),e.getMsg());
        } catch (Exception e){
            return BaseResponse.error("500",e.getMessage());
        }
        return BaseResponse.ok();
    }
}
